
//
//  MainViewController.m
//  Time
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "MainViewController.h"
#import "Main_TableView_Cell.h"
#import "SCB_Today_ViewController.h"
#import "SearchViewController.h"

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) UITableView *mainTableView;
/**
 *  存放标题名字的数组
 */
@property (nonatomic,retain) NSArray *nameArray;
/**
 *  headerView
 */
@property (nonatomic,retain) UIView *headerView;
/**
 *  搜索按钮
 */
@property (nonatomic,retain) UIButton *searchButton;

@end

@implementation MainViewController

-(void)dealloc{
    [_mainTableView release];
    [_nameArray release];
    [_VCArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.nameArray = @[@"今日 · TODAY",@"电台 · RADIO",@"阅读 · READING",@"音乐 · MUSIC",@"良品 · PRODUCTS"];
    self.nameArray = @[@"今日 · TODAY",@"电台 · RADIO",@"良品 · PRODUCTS"];
//    self.VCArray = [NSArray array];
    
    
    [self createView];
    [self createHeaderView];
}


-(void)createView{
    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT - 88) style:UITableViewStylePlain];
    self.mainTableView.dataSource = self;
    self.mainTableView.delegate = self;
    
    
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"26.jpg"]];
    [self.view addSubview:imageView];
    [imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.top.equalTo(self.view.mas_top).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
    }];
    imageView.frame = CGRectMake(0, 0, SWIDTH, SHEIGHT - 88);
    self.mainTableView.backgroundColor = [UIColor clearColor];
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.mainTableView];
    [self.mainTableView registerClass:[Main_TableView_Cell class] forCellReuseIdentifier:@"main_tableView_Cell"];
    [imageView release];
    [_mainTableView release];
}

-(void)createHeaderView{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SWIDTH * 0.2)];
    self.mainTableView.tableHeaderView = self.headerView;
//    self.headerView.backgroundColor = [UIColor cyanColor];
    self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headerView addSubview:self.searchButton];
    [self.searchButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView.mas_left).offset(9);
        make.top.equalTo(self.headerView.mas_top).offset(9);
        make.bottom.equalTo(self.headerView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    UIView *stripView = [[UIView alloc] init];
    stripView.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:stripView];
    [stripView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView.mas_left).offset(9);
        make.right.equalTo(self.headerView.mas_right).offset(-9);
        make.bottom.equalTo(self.headerView.mas_bottom).offset(0);
        make.height.mas_equalTo(2);
    }];
    [self.searchButton setImage:[UIImage imageNamed:@"搜索.png"] forState:UIControlStateNormal];
    [self.searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)searchButtonAction:(UIButton *)button{
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self presentViewController:searchVC animated:YES completion:^{
        NSLog(@"跳转至搜索页面");
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    /**
     *  主视图cell数,每一个cell代表一个VC.
     */
    return self.nameArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Main_TableView_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"main_tableView_Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.titleName = [self.nameArray objectAtIndex:indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
//    SCB_Today_ViewController *today_[self.VCArray objectAtIndex:indexPath.row]VC = ;
    [self presentViewController:[self.VCArray objectAtIndex:indexPath.row] animated:YES completion:^{
        
    }];
}

-(BOOL)prefersStatusBarHidden{
    /**
     *  此方法隐藏状态栏
     */
    return YES;
}

-(void)setVCArray:(NSArray *)VCArray{
    if (_VCArray != VCArray){
        [_VCArray release];
        _VCArray = [VCArray retain];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
